using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections;
using BE;

namespace DAL//ModulDuoc.BusinessLayer.DataLayer

{

	/// <summary>
	/// Data access layer class for ZD_DonMauCT
	/// </summary>
	public class ZD_DonMauCTSql : DataLayerBase 
	{

        #region Constructor

		/// <summary>
		/// Class constructor
		/// </summary>
		public ZD_DonMauCTSql()
		{
			// Nothing for now.
		}

        #endregion

        #region Public Methods

        /// <summary>
        /// insert new row in the table
        /// </summary>
		/// <param name="businessObject">business object</param>
		/// <returns>true of successfully insert</returns>
		public bool Insert(ZD_DonMauCT businessObject)
		{
			SqlCommand	sqlCommand = new SqlCommand();
			sqlCommand.CommandText = "dbo.[zD_DonMauCT_Insert]";
			sqlCommand.CommandType = CommandType.StoredProcedure;

			// Use connection object of base class
			sqlCommand.Connection = MainConnection;

			try
			{
                
				sqlCommand.Parameters.Add(new SqlParameter("@DonMauCT_ID", SqlDbType.Int, 4, ParameterDirection.Output, false, 0, 0, "", DataRowVersion.Proposed, businessObject.DonMauCT_ID));
				sqlCommand.Parameters.Add(new SqlParameter("@DonMau_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.DonMau_ID));
				sqlCommand.Parameters.Add(new SqlParameter("@MaVT", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.MaVT));
				sqlCommand.Parameters.Add(new SqlParameter("@TenVT", SqlDbType.NVarChar, 150, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.TenVT));
				sqlCommand.Parameters.Add(new SqlParameter("@SoLuong", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.SoLuong));
				sqlCommand.Parameters.Add(new SqlParameter("@DonViTinh", SqlDbType.NVarChar, 10, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.DonViTinh));
				sqlCommand.Parameters.Add(new SqlParameter("@LieuDung", SqlDbType.NVarChar, 150, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.LieuDung));

								
				MainConnection.Open();
				
				sqlCommand.ExecuteNonQuery();
                businessObject.DonMauCT_ID = (int)sqlCommand.Parameters["@DonMauCT_ID"].Value;

				return true;
			}
			catch(Exception ex)
			{				
				throw new Exception("ZD_DonMauCT::Insert::Error occured.", ex);
			}
			finally
			{			
				MainConnection.Close();
				sqlCommand.Dispose();
			}
		}

         /// <summary>
        /// update row in the table
        /// </summary>
        /// <param name="businessObject">business object</param>
        /// <returns>true for successfully updated</returns>
        public bool Update(ZD_DonMauCT businessObject)
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[zD_DonMauCT_Update]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {
                
				sqlCommand.Parameters.Add(new SqlParameter("@DonMauCT_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.DonMauCT_ID));
				sqlCommand.Parameters.Add(new SqlParameter("@DonMau_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.DonMau_ID));
				sqlCommand.Parameters.Add(new SqlParameter("@MaVT", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.MaVT));
				sqlCommand.Parameters.Add(new SqlParameter("@TenVT", SqlDbType.NVarChar, 150, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.TenVT));
				sqlCommand.Parameters.Add(new SqlParameter("@SoLuong", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.SoLuong));
				sqlCommand.Parameters.Add(new SqlParameter("@DonViTinh", SqlDbType.NVarChar, 10, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.DonViTinh));
				sqlCommand.Parameters.Add(new SqlParameter("@LieuDung", SqlDbType.NVarChar, 150, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.LieuDung));

                
                MainConnection.Open();

                sqlCommand.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("ZD_DonMauCT::Update::Error occured.", ex);
            }
            finally
            {
                MainConnection.Close();
                sqlCommand.Dispose();
            }
        }

        /// <summary>
        /// Delete by primary key
        /// </summary>
        /// <param name="keys">primary keys</param>
        /// <returns>true for successfully deleted</returns>
        public bool Delete(ZD_DonMauCTKeys keys)
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[zD_DonMauCT_DeleteByPrimaryKey]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

				sqlCommand.Parameters.Add(new SqlParameter("@DonMauCT_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, keys.DonMauCT_ID));


                MainConnection.Open();

                sqlCommand.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("ZD_DonMauCT::DeleteByKey::Error occured.", ex);
            }
            finally
            {                
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }

        /// <summary>
        /// Delete records by field
        /// </summary>
        /// <param name="fieldName">name of field</param>
        /// <param name="value">value of field</param>
        /// <returns>true for successfully deleted</returns>
        public bool DeleteByField(string fieldName, object value)
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[zD_DonMauCT_DeleteByField]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

                sqlCommand.Parameters.Add(new SqlParameter("@FieldName", fieldName));
                sqlCommand.Parameters.Add(new SqlParameter("@Value", value));
                
                MainConnection.Open();

                sqlCommand.ExecuteNonQuery();

                return true;

            }
            catch (Exception ex)
            {                
                throw new Exception("ZD_DonMauCT::DeleteByField::Error occured.", ex);
            }
            finally
            {             
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }
        /// <summary>
        /// Select by primary key
        /// </summary>
        /// <param name="keys">primary keys</param>
        /// <returns>ZD_DonMauCT business object</returns>
        public ZD_DonMauCT SelectByPrimaryKey(ZD_DonMauCTKeys keys)
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[zD_DonMauCT_SelectByPrimaryKey]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

				sqlCommand.Parameters.Add(new SqlParameter("@DonMauCT_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, keys.DonMauCT_ID));

                
                MainConnection.Open();

                IDataReader dataReader = sqlCommand.ExecuteReader();

                if (dataReader.Read())
                {
                    ZD_DonMauCT businessObject = new ZD_DonMauCT();

                    PopulateBusinessObjectFromReader(businessObject, dataReader);

                    return businessObject;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("ZD_DonMauCT::SelectByPrimaryKey::Error occured.", ex);
            }
            finally
            {             
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }


        /// <summary>
        /// Select all rescords
        /// </summary>
        /// <returns>list of ZD_DonMauCT</returns>
        public List<ZD_DonMauCT> SelectAll_List()
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[zD_DonMauCT_SelectAll]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {
                
                MainConnection.Open();

                IDataReader dataReader = sqlCommand.ExecuteReader();

                return PopulateObjectsFromReader(dataReader);

            }
            catch (Exception ex)
            {                
                throw new Exception("ZD_DonMauCT::SelectAll::Error occured.", ex);
            }
            finally
            {
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }

        /// <summary>
        /// Select records by field
        /// </summary>
        /// <param name="fieldName">name of field</param>
        /// <param name="value">value of field</param>
        /// <returns>list of ZD_DonMauCT</returns>
        public List<ZD_DonMauCT> SelectByField_List(string fieldName, object value)
        {

            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[zD_DonMauCT_SelectByField]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

                sqlCommand.Parameters.Add(new SqlParameter("@FieldName", fieldName));
                sqlCommand.Parameters.Add(new SqlParameter("@Value", value));

                MainConnection.Open();
                
                IDataReader dataReader = sqlCommand.ExecuteReader();

                return PopulateObjectsFromReader(dataReader);

            }
            catch (Exception ex)
            {
                throw new Exception("ZD_DonMauCT::SelectByField::Error occured.", ex);
            }
            finally
            {

                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }

//thêm hiennv

        /// <summary>
        /// Select all rescords
        /// </summary>
        /// <returns>dataTable</returns>
        public DataTable SelectAll()
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[zD_DonMauCT_SelectAll]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {
                
                MainConnection.Open();

                DataTable ds = new DataTable();
                SqlDataAdapter myadapter = new SqlDataAdapter(sqlCommand);
                myadapter.Fill(ds);

                return ds;

            }
            catch (Exception ex)
            {                
                throw new Exception("ZD_DonMauCT::SelectAll::Error occured.", ex);
            }
            finally
            {
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }

        /// <summary>
        /// Select records by field
        /// </summary>
        /// <param name="fieldName">name of field</param>
        /// <param name="value">value of field</param>
        /// <returns>dataTable</returns>
        public DataTable SelectByField(string fieldName, object value)
        {

            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[zD_DonMauCT_SelectByField]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

                sqlCommand.Parameters.Add(new SqlParameter("@FieldName", fieldName));
                sqlCommand.Parameters.Add(new SqlParameter("@Value", value));

                MainConnection.Open();
                
                DataTable ds = new DataTable();
                SqlDataAdapter myadapter = new SqlDataAdapter(sqlCommand);
                myadapter.Fill(ds);

                return ds;

            }
            catch (Exception ex)
            {
                throw new Exception("ZD_DonMauCT::SelectByField::Error occured.", ex);
            }
            finally
            {

                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }     
//end thêm hiennv
        #endregion

        #region Private Methods

        /// <summary>
        /// Populate business object from data reader
        /// </summary>
        /// <param name="businessObject">business object</param>
        /// <param name="dataReader">data reader</param>
        internal void PopulateBusinessObjectFromReader(ZD_DonMauCT businessObject, IDataReader dataReader)
        {


				businessObject.DonMauCT_ID = dataReader.GetInt32(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.DonMauCT_ID.ToString()));

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.DonMau_ID.ToString())))
				{
					businessObject.DonMau_ID = dataReader.GetInt32(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.DonMau_ID.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.MaVT.ToString())))
				{
					businessObject.MaVT = dataReader.GetInt32(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.MaVT.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.TenVT.ToString())))
				{
					businessObject.TenVT = dataReader.GetString(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.TenVT.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.SoLuong.ToString())))
				{
					businessObject.SoLuong = dataReader.GetInt32(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.SoLuong.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.DonViTinh.ToString())))
				{
					businessObject.DonViTinh = dataReader.GetString(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.DonViTinh.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.LieuDung.ToString())))
				{
					businessObject.LieuDung = dataReader.GetString(dataReader.GetOrdinal(ZD_DonMauCT.ZD_DonMauCTFields.LieuDung.ToString()));
				}


        }

        /// <summary>
        /// Populate business objects from the data reader
        /// </summary>
        /// <param name="dataReader">data reader</param>
        /// <returns>list of ZD_DonMauCT</returns>
        internal List<ZD_DonMauCT> PopulateObjectsFromReader(IDataReader dataReader)
        {

            List<ZD_DonMauCT> list = new List<ZD_DonMauCT>();

            while (dataReader.Read())
            {
                ZD_DonMauCT businessObject = new ZD_DonMauCT();
                PopulateBusinessObjectFromReader(businessObject, dataReader);
                list.Add(businessObject);
            }
            return list;

        }

        #endregion

	}
}
